<?php
// load Wordpress
require_once('../../../wp-load.php');
require_once('../../../wp-admin/includes/admin.php');
do_action('admin_init');

// load AjaxTableEditor
require_once('mate/php/lang/LangVars-en.php');
require_once('mate/php/AjaxTableEditor.php');
require_once('reg_table.php');

class RegAjax
{
	function logError($message, $file, $line)
	{
		$message = sprintf($this->langVars->errInScript,$file,$line,$message);
		var_dump($message);
		die;
	}
	
	function initiateEditor()
	{
		global $current_user;
		get_currentuserinfo();
		$first_name = get_usermeta($current_user->ID,'first_name');
		$last_name = get_usermeta($current_user->ID,'last_name');
		$locality = get_usermeta($current_user->ID,'locality');
		$phone = get_usermeta($current_user->ID,'phone');
		$email = $current_user->user_email;
		

        // Select this for open registraion for all types
/*		$tableColumns['reg_type'] = array(  'display_text' => 'Reg. Type', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => true, 
                                            'select_array' => array( '3 day full' => '3 Day Full-time', '3 day part' => '3 Day Part-time', 'monthly' => 'Monthly Only'),
                                            'col_header_info' => 'class="tooltip" title="Registration Type"', 
                                            'input_info' => 'class="tooltip" title="Choose the registration type."' );*/
		$tableColumns['reg_type'] = array(  'display_text' => 'Reg. Type', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => true, 
                                            'select_array' => array('monthly' => 'Monthly Only'),
                                            'col_header_info' => 'class="tooltip" title="Registration Type"', 
                                            'input_info' => 'class="tooltip" title="Choose the registration type."' );

                                          
                                          
		$tableColumns['new'] = array(   'display_text' => 'New', 
                                        'perms' => 'EVCTAXQSHO', 
                                        'req' => true, 
                                        'display_mask' => "if(registrations7.new, 'Yes', 'No')",
                                        'select_array' => array(0 => 'No', 1 => 'Yes'), 
                                        'default' => 0,
                                        'col_header_info' => 'class="tooltip" title="New Trainee"',
                                        'input_info' => 'class="tooltip" title="Indicate if trainee is new (Yes) or is continuing (No)"' );
                                        
		$tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'VQSXO'); 
        
		$tableColumns['first_name'] = array(    'display_text' => 'First', 
                                                'perms' => 'EVCTAXQSHO', 
                                                'req' => true, 
                                                'default' => $first_name,
                                                'textarea' => array('rows' => 1, 'cols'=> 40),
                                                'col_header_info' => 'class="tooltip" title="First Name"', 
                                                'input_info' => 'class="tooltip" title="Defaults to your name. Change for other registraitons"' );
                                                
		$tableColumns['last_name'] = array( 'display_text' => 'Last', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => true, 
                                            'default' => $last_name,
                                            'textarea' => array('rows' => 1, 'cols'=> 40),
                                            'col_header_info' => 'class="tooltip" title="Last Name"', 
                                            'input_info' => 'class="tooltip" title="Defaults to your name. Change for other registrations"' );
                                           
		$tableColumns['age'] = array(   'display_text' => 'Age', 
                                        'perms' => 'EVCTAXQSHO', 
                                        'req' => true, 
                                        'display_mask' => "CASE registrations7.age WHEN 25 THEN '1-25' WHEN 35 THEN '26-35' WHEN 45 THEN '36-45' WHEN 46 THEN '46-99' END", 
                                        'select_array' => array(25 => '< 25', 35 => '26 - 35', 45 => '36 - 45', 46 => '> 46'),
                                        'col_header_info' => 'class="tooltip" title="Age Range"', 
                                        'input_info' => 'class="tooltip" title="Select the age range of the registrant."');
                                        
		$tableColumns['bro_sis'] = array(   'display_text' => 'Bro/Sis', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => true, 
                                            'select_array' => array('B' => 'Brother', 'S' => 'Sister'),
                                            'col_header_info' => 'class="tooltip" title="Brother or Sister"', 
                                            'input_info' => 'class="tooltip" title="Brother or sister."');
                                            
		$tableColumns['email'] = array(     'display_text' => 'Email', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'default' => $email,
                                            'textarea' => array('rows' => 1, 'cols'=> 40),
                                            'col_header_info' => 'class="tooltip" title="Email"', 
                                            'input_info' => 'class="tooltip" title="Defaults to your email. Change for other registrations. Not required."' );
                                            
		$tableColumns['phone'] = array(     'display_text' => 'Phone', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'default' => $phone,
                                            'textarea' => array('rows' => 1, 'cols'=> 40),
                                            'col_header_info' => 'class="tooltip" title="Phone"', 
                                            'input_info' => 'class="tooltip" title="Defaults to your phone. Change for other registrations. Not required."' );
                                            
		$tableColumns['locality'] = array(  'display_text' => 'Locality', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => true, 
                                            'join' => array('table' => 'localities', 'column' => 'id', 'display_mask' => 'localities.city', 'type' => 'left'), 
                                            'default' => $locality,
                                            'col_header_info' => 'class="tooltip" title="Locality"', 
                                            'input_info' => 'class="tooltip" title="Select the registrant\'s locality."' );
                                            
		$tableColumns['room_type'] = array( 'display_text' => '3-day Room', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'select_array' => array('basic' => 'Basic Accomodations', 'moderate' => 'Moderate Accomodations', 'deluxe' => 'Deluxe Accomodations'),
                                            'col_header_info' => 'class="tooltip" title="Room Type for 3-day"', 
                                            'input_info' => 'class="tooltip" title="If registering for the 3-day training, choose the room type."' ); 


        $tableColumns['reg_days'] = array(   'display_text' => 'Reg. Days', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => false, 
                                            'select_array' => array(    'FRI,SAT' => 'Friday and Saturday only', 
                                                                        'FRI,LD' => 'Friday and Lord\'s Day only',
                                                                        'SAT,LD' => 'Saturday and Lord\'s Day only',
                                                                        'FRI' => 'Friday only',
                                                                        'SAT' => 'Saturday only',
                                                                        'LD' => 'Lord\'s Day only'),
                                            'col_header_info' => 'class="tooltip" title="3-Day Part Time Selection"', 
                                            'input_info' => 'class="tooltip" title="If registering for 3 day part time, please select the days joining."');                                            
		$tableColumns['registrator'] = array(   'display_text' => 'Registrator', 
                                                'perms' => 'VCAXQ', 
                                                'join' => array('table' => 'wp_users', 'column' => 'ID', 'display_mask' => 'wp_users.display_name', 'type' => 'left'), 
                                                'default' => $current_user->ID, 
                                                'hidden_add' => true, 
                                                'hidden_edit' => true,
                                                'col_header_info' => 'class="tooltip" title="User who made this registration."', 
                                                'input_info' => 'class="tooltip" title="User who made this registration."' );
                                                
		$tableColumns['reg_time'] = array(  'display_text' => 'Reg. Time', 
                                            'perms' => 'VQSXO',
                                            'col_header_info' => 'class="tooltip" title="Time the registration was made."',
                                            'input_info' => 'class="tooltip" title="Time this registration was made."');
                                          
		$tableColumns['language'] = array(  'display_text' => 'Language', 
                                            'perms' => 'EVCTAXQSHO', 
                                            'req' => true, 
                                            'select_array' => array('English' => 'English', 'Chinese' => 'Chinese', 'Spanish' => 'Spanish', 'Korean' => 'Korean'), 
                                            'default' => 'English',
                                            'col_header_info' => 'class="tooltip" title="Meeting language"',
                                            'input_info' => 'class="tooltip" title="Select the language of the training meetings the registrant would like to attend."');
                                            
		$tableColumns['binder_lang'] = array(   'display_text' => 'Binder', 
                                                'perms' => 'EVCTAXQSHO', 
                                                'req' => false, 
                                                'select_array' => array('None'=>'None', 'English' => 'English', 'Chinese' => 'Chinese', 'Spanish' => 'Spanish', 'Korean' => 'Korean'), 
                                                'default' => 'None',
                                                'col_header_info' => 'class="tooltip" title="Binder Language"',
                                                'input_info' => 'class="tooltip" title="Select the language of the primary GOW binder the registrant would like."');
                                                
		$tableColumns['binder2_lang'] = array(  'display_text' => 'Binder2', 
                                                'perms' => 'EVCTAXQSHO', 
                                                'req' => false, 
                                                'select_array' => array('None'=>'None', 'English' => 'English', 'Chinese' => 'Chinese', 'Spanish' => 'Spanish', 'Korean' => 'Korean'), 
                                                'default' => 'None',
                                                'col_header_info' => 'class="tooltip" title="Binder Language"',
                                                'input_info' => 'class="tooltip" title="Select the language of the secondary GOW binder the registrant would like."');		
                                                
        $tableColumns['district'] = array(      'display_text' => 'District', 
                                                'perms' => 'EVCTAXQSHO', 
                                                'req' => true, 
                                                'join' => array('table' => 'districts', 'column' => 'id', 'display_mask' => 'districts.name', 'type' => 'left'),
                                                'col_header_info' => 'class="tooltip" title="District"', 
                                                'input_info' => 'class="tooltip" title="Select the registrant\'s district."' );
                                                
		$tableColumns['roomate_req'] = array(   'display_text' => 'Roomate', 
                                                'perms' => 'EVCAXQSHO', 
                                                'textarea' => array('rows' => 1, 'cols'=> 40),
                                                'col_header_info' => 'class="tooltip" title="Roomate Request"', 
                                                'input_info' => 'class="tooltip" title="Request for certain roomates during the 3-day training. Not required."' );
                                       
		$tableColumns['needs'] = array( 'display_text' => 'Special Needs', 
                                        'perms' => 'EVCAXQSHO', 
                                        'textarea' => array('rows' => 3, 'cols'=> 40),
                                        'col_header_info' => 'class="tooltip" title="Special Needs during the 3 Day training"', 
                                        'input_info' => 'class="tooltip" title="Special Needs during the 3 Day training. Not required."' );
                                        
		$tableName = 'registrations7';
		$primaryCol = 'id';
		$errorFun = array(&$this,'logError');
		// Disable edit, add, delete, and copy
		//$permissions = 'UVMQSXHO'; // This disables ability for users to register.
		$permissions = 'UEAVMDQCSXHO';  // This allows users to register (add, edit, delete, copy)
        
		$this->Editor = new RegTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns);
		$this->Editor->setConfig('varPrefix','regView4');
		$this->Editor->setConfig('tableTitle', "$first_name $last_name's Registrations");
		$this->Editor->setConfig('tableInfo','cellpadding="1" class="mateTable"');
		$this->Editor->setConfig('orderByColumn','reg_type');
		$this->Editor->setConfig('addRowTitle','Add Registration');
		$this->Editor->setConfig('editRowTitle','Edit Registration');
		
		// only allow registrations made by this user
		$this->Editor->setConfig('sqlFilters',"registrator = '$current_user->ID'");
	}

	function RegAjax()
	{
		if ( ! is_user_logged_in() ) {
			$this->logError('User needs to be logged in',__FILE__, __LINE__);
		}
		if ( !current_user_can('read') ) {
			$this->logError('Reg error: user does not have read permissions',__FILE__, __LINE__);
		}
		if(isset($_POST['json']))
		{
			session_start();
			// Initiating lang vars here is only necessary for the logError, and mysqlConnect functions in Common.php.
			// If you are not using Common.php or you are using your own functions you can remove the following line of code.
			$this->langVars = new LangVars();

			if(ini_get('magic_quotes_gpc'))
			{
				$_POST['json'] = stripslashes($_POST['json']);
			}
			if(function_exists('json_decode'))
			{
				$data = json_decode($_POST['json']);
			}
			else
			{
				require_once('/mate/php/JSON.php');
				$js = new Services_JSON();
				$data = $js->decode($_POST['json']);
			}
			if(empty($data->info) && strlen(trim($data->info)) == 0)
			{
				$data->info = '';
			}
			$this->initiateEditor();
			$this->Editor->main($data->action,$data->info);
			if(function_exists('json_encode'))
			{
				echo json_encode($this->Editor->retArr);
			}
			else
			{
				echo $js->encode($this->Editor->retArr);
			}
		}
		else if(isset($_GET['export']))
		{
			session_start();
			ob_start();
			$this->initiateEditor();
			echo $this->Editor->exportInfo();
			header("Cache-Control: no-cache, must-revalidate");
			header("Pragma: no-cache");
			header("Content-type: application/x-msexcel");
			header('Content-Type: text/csv');
			header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
			exit();
		}
	}
}

$regAjax = new RegAjax();